package com.ah.shanxi_demo.service.impl;

import com.ah.shanxi_demo.common.enums.OrderCommonEnum;
import com.ah.shanxi_demo.common.enums.OrderTypeEnum;
import com.ah.shanxi_demo.common.enums.PolicyDeleteEnum;
import com.ah.shanxi_demo.entity.SxOrderReceipt;
import com.ah.shanxi_demo.mapper.SxOrderMapper;
import com.ah.shanxi_demo.mapper.SxOrderReceiptMapper;
import com.ah.shanxi_demo.param.input.ReceiptPO;
import com.ah.shanxi_demo.service.IOrderService;
import com.ah.shanxi_demo.service.IReceiptService;
import com.ah.shanxi_demo.sync.param.result.SyncReceiptResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ah.shanxi_demo.convert.ConvertOrderReceipt.convert_sx_order_receipt;
import static com.ah.shanxi_demo.convert.ConvertOrderReceipt.convert_sync_receipt_po;
import static com.ah.shanxi_demo.sync.client.SyncReceiptClient.receipt_client;

/**
 * @author zt 2020/8/25
 */

@Service
public class ReceiptService implements IReceiptService {
    private static final Logger log = LoggerFactory.getLogger(RegisterService.class);


    @Autowired
    private SxOrderReceiptMapper receiptMapper;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private SxOrderMapper orderMapper;

    /**
     * 根据工单id 查询是否存在回执，不存在创建 存在直接返回存在的回执
     *
     * @param sxOrderReceipt
     * @return
     */
    @Override
    @Transactional
    public SxOrderReceipt insertByOrderReceipt(SxOrderReceipt sxOrderReceipt) {

        SxOrderReceipt orderReceipt = receiptMapper.selectByOrderId(sxOrderReceipt.getOrderId());
        if (null == orderReceipt) {
            try{
                Boolean orderReceiptBoolean = receiptMapper.insertOrderReceipt(sxOrderReceipt);
            }catch (Throwable e){
                log.error("ReceiptService 【insertByOrderReceipt】" + e.getMessage());
            }

        }
        return sxOrderReceipt;
    }

    @Override
    public Boolean news(ReceiptPO receiptPO) {
        //TODO 逻辑业务编写
        return null;
    }


    @Override
    public Boolean receipt(ReceiptPO receiptPO) {

        //获取根据工单id 收集部级平台回执接口参数
        SyncReceiptResult syncReceiptResult = receiptMapper.selectSyncReceiptResult(receiptPO.getOrderId());

        //转换传参格式 拼装参数 receiptPO-->SyncReceiptResult
        SyncReceiptResult syncReceiptResultAll = convert_sync_receipt_po(syncReceiptResult, receiptPO);


        //同步部级平台回执接口进行 故障数据上传
        Boolean receipt_client = receipt_client(syncReceiptResultAll);

        if (receipt_client) {
            //部级平台上传成功 插入回执表数据库信息 并改变工单状态
            SxOrderReceipt sxOrderReceipt = convert_sx_order_receipt(receiptPO);//类型转换

            sxOrderReceipt.setOrderStatus(OrderCommonEnum.STATUS_SUCCESS.getKeyInt());
            sxOrderReceipt.setDelete(PolicyDeleteEnum.UN_DELETE.getKeyInt());
            insertByOrderReceipt(sxOrderReceipt);


            //更新工单表状态 1 3 4 回执成功
            if (syncReceiptResultAll.getType() == OrderTypeEnum.ORDER_TYPE_1.getKeyInt() ||
                    syncReceiptResultAll.getType() == OrderTypeEnum.ORDER_TYPE_3.getKeyInt() ||
                    syncReceiptResultAll.getType() == OrderTypeEnum.ORDER_TYPE_4.getKeyInt()
            ) {

                Boolean handleOrderStatus = orderService.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_SUCCESS);

                if (!handleOrderStatus) {
                    orderService.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_FAIL);
                }

            } else {
                Boolean handleOrderStatus = orderService.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_UNDONE);
                if (!handleOrderStatus) {
                    orderService.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_FAIL);
                }
            }

        }

        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean receiptSimulation(ReceiptPO receiptPO) {
        //获取根据工单id 收集部级平台回执接口参数
        SyncReceiptResult syncReceiptResult = receiptMapper.selectSyncReceiptResult(receiptPO.getOrderId());

        //转换传参格式 拼装参数 receiptPO-->SyncReceiptResult
        SyncReceiptResult syncReceiptResultAll = convert_sync_receipt_po(syncReceiptResult, receiptPO);

        //类型转换
        SxOrderReceipt sxOrderReceipt = convert_sx_order_receipt(receiptPO);

        sxOrderReceipt.setOrderStatus(OrderCommonEnum.STATUS_SUCCESS.getKeyInt());
        sxOrderReceipt.setDelete(PolicyDeleteEnum.UN_DELETE.getKeyInt());
        insertByOrderReceipt(sxOrderReceipt);

        //更新工单表状态 1 3 4 回执成功
        if (syncReceiptResultAll.getType() == OrderTypeEnum.ORDER_TYPE_1.getKeyInt() ||
                syncReceiptResultAll.getType() == OrderTypeEnum.ORDER_TYPE_3.getKeyInt() ||
                syncReceiptResultAll.getType() == OrderTypeEnum.ORDER_TYPE_4.getKeyInt()
        ) {

            Boolean handleOrderStatus=orderMapper.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_SUCCESS.getKeyInt());

            if (!handleOrderStatus) {
                orderMapper.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_FAIL.getKeyInt());
            }

        } else {
            Boolean handleOrderStatus=orderMapper.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_UNDONE.getKeyInt());
            if (!handleOrderStatus) {
                orderMapper.handleOrderStatus(sxOrderReceipt.getOrderId(), OrderCommonEnum.STATUS_FAIL.getKeyInt());
            }
        }
        return Boolean.TRUE;

    }

    @Override
    public SxOrderReceipt selectById(Long orderId) {
        SxOrderReceipt model = receiptMapper.selectById(orderId);
        if (model==null){
            return new SxOrderReceipt();
        }
        return model;
    }
}
